<?php
/*
 * @Author: 李志刚
 * @CopyRight: 2020-2030 衡水山木枝技术服务有限公司
 * @Date: 2021-06-10 17:32:04
 * @Description: 订单主表
 * @LastEditors: 李志刚
 * @LastEditTime: 2021-07-06 09:06:24
 * @FilePath: /qingshanERP/app/Models/QingShanERP/Order.php
 */

namespace App\Models\QingShanERP;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Order extends Model
{
    use HasFactory;
    /**
     * 关联到模型的数据表
     *
     * @var string
     */
    protected $table = 'orders';

    // 不可以批量赋值的字段，为空则表示都可以
    protected $guarded = [];

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = [];

    /**
     * 表明模型是否应该被打上时间戳
     *
     * @var bool
     */
    public $timestamps = true;
    // 默认展开
    protected $appends = ['_expanded', 'trade_type_name', 'confirm_status_name', 'order_status_name', 'supplier_order_status_name', 'after_status'];
    public function getExpandedAttribute()
    {
        return true;
    }
    public function getTradeTypeNameAttribute()
    {
        switch ($this->trade_type) {
            case '1':
                $name = '定金订单';
                break;
            
            default:
                $name = '普通订单';
                break;
        }
        return $name;
    }
    public function getConfirmStatusNameAttribute()
    {
        switch ($this->confirm_status) {
            case '2':
                $name = '已取消';
                break;

            case '1':
                $name = '已成交';
                break;

            default:
                $name = '未成交';
                break;
        }
        return $name;
    }
    public function getOrderStatusNameAttribute()
    {
        switch ($this->order_status) {
            case '3':
                $name = '已签收';
                break;

            case '2':
                $name = '待签收';
                break;

            default:
                $name = '待发货';
                break;
        }
        return $name;
    }
    public function getSupplierOrderStatusNameAttribute()
    {
        switch ($this->supplier_order_status) {
            case '1':
                $name = '已发货';
                break;

            default:
                $name = '未发货';
                break;
        }
        return $name;
    }
    public function getAfterStatusAttribute()
    {
        return config('pdd.after_sales_status')[$this->after_sales_status];
    }
    /**
     * 应该被转换成原生类型的属性。
     *
     * @var array
     */
    protected $casts = [];
    // 关联门店
    public function shop()
    {
        return $this->belongsTo('\App\Models\QingShanERP\Shop', 'shop_id', 'id');
    }
    // 关联供应商
    public function supplier()
    {
        return $this->belongsTo('\App\Models\QingShanERP\Supplier', 'supplier_id', 'id');
    }
    // 关联供应商
    public function supplier_staff()
    {
        return $this->belongsTo('\App\Models\QingShanERP\SupplierStaff', 'supplier_staff_id', 'id');
    }
    // 关联数据附表
    public function pdd()
    {
        return $this->hasOne('\App\Models\QingShanERP\OrderPddData', 'order_id', 'id');
    }
    public function goods()
    {
        return $this->hasMany('\App\Models\QingShanERP\OrderGood', 'order_id', 'id');
    }
    public function tracking()
    {
        return $this->hasMany('\App\Models\QingShanERP\OrderPddTracking', 'order_id', 'id');
    }
}
